﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace visu.Controllers
{
    public class SettingController : Controller
    {
        //
        // GET: /Setting/

        public ActionResult Index()
        {
            return View();
        }


        #region ---------------------------------账号管理----------------------------------------------
        public ActionResult Account()
        {
            return View();
        }
        
        public ActionResult AccountDetail(int page, int rows, user user, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "USER_ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);

            Expression<Func<user, bool>> exp = SearchExpression.GetSearchExpression<user>(user, listSearch, SearchContains, "USERNAME", "NAME", "ROLE");

            var list = BLLSession.Current.userBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.userDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AccountEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            user userinfo = new user();
            if (action == "add")
            {
                Id = 0; //Commons.String2Int32(BLLSession.Current.userBLL.AddModelReturnId(userinfo, "USER_ID"));
                ViewData["role"] = "";
            }
            else
            {
                userinfo = BLLSession.Current.userBLL.GetListBy(c => c.USER_ID == Id).FirstOrDefault();
                ViewData["role"] = userinfo.ROLE;
            }

            ViewData.Model = userinfo;
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;

            return View();
        }
        public ActionResult AccountResert()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            user userinfo = new user();
            if (action == "add")
            {
                Id = 0; //Commons.String2Int32(BLLSession.Current.userBLL.AddModelReturnId(userinfo, "USER_ID"));
            }
            else
            {
                userinfo = BLLSession.Current.userBLL.GetListBy(c => c.USER_ID == Id).FirstOrDefault();
            }

            ViewData.Model = userinfo;
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult DeleteUserById(int Id)
        {
            BLLSession.Current.userBLL.DelBy((d => d.USER_ID == Id));
            Message msg = new Message() { state = "success" };
            return Json(msg, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetAccountByUserId(int Id)
        {
            var user = BLLSession.Current.userBLL.GetListBy(c => c.USER_ID == Id).FirstOrDefault();
            return Json(user, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AddAccount(user user)
        {
            user.CREATED_BY = OperateContext.userInfo.NAME;
            user.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            user.COMPANY_ID = OperateContext.userInfo.COMPANY_ID;
            BLLSession.Current.userBLL.Add(user);
            return Json(new Message() { state = "success" });
        }

        public ActionResult EditAccount(user user)
        {
            BLLSession.Current.userBLL.Modify(user, "NAME", "ROLE", "PHONE", "IS_ACTIVE", "USER_TYPE");//CREATED_TIME
            return Json(new Message() { state = "success" });
        }
        public ActionResult EditPassword(user user)
        {
            BLLSession.Current.userBLL.Modify(user, "PASSWORD");
            return Json(new Message() { state = "success" });
        }
        #endregion

        #region ---------------------------------未认证用户----------------------------------------------
        public ActionResult AccountUnregistered()
        {
            return View();
        }

        public ActionResult AccountUnregisteredDetail(int page, int rows, usersunregistered user, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "USER_ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);
            SearchCondition condition1 = new SearchCondition() { Field = "STATUS", Operator = "=", Value = "1" };
            listSearch.Add(condition1);
            Expression<Func<usersunregistered, bool>> exp = SearchExpression.GetSearchExpression<usersunregistered>(user, listSearch, SearchContains, "PHONE", "NAME");

            var list = BLLSession.Current.usersunregisteredBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.usersunregisteredDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult VerifyToCompany(int Id)
        {
            usersunregistered uuser = new usersunregistered();
            user user = new user();
            uuser=BLLSession.Current.usersunregisteredBLL.GetListBy(c => c.USER_ID == Id&&c.STATUS==1).FirstOrDefault();
            if (uuser == null)
            {
                return Json(new Message() { state = "error",msg="用户信息不存在！" });
            }
            else
            {
                user.USERNAME = uuser.USERNAME;
                user.EMPLOYERCOMPANY = uuser.EMPLOYERCOMPANY;
                user.COMPANY_ID = uuser.COMPANY_ID;
                user.PASSWORD = uuser.PASSWORD;
                user.NAME = uuser.NAME;
                user.ROLE = "用户";
                user.PHONE = uuser.PHONE;
                user.IS_ACTIVE = "是";
                user.USER_TYPE = "1";
                user.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                user.CREATED_BY = OperateContext.userInfo.NAME.ToString();
                BLLSession.Current.userBLL.Add(user);
                uuser.STATUS=2;
                BLLSession.Current.usersunregisteredBLL.ModifyBy(uuser, c => c.USER_ID == Id, "STATUS");
                return Json(new Message() { state = "success", msg = "认证成功！" });
            }
        }
        #endregion

        #region---------------------------------权限管理----------------------------------------------
        public ActionResult RolePower()
        {
            ViewData["company"] = OperateContext.userInfo.COMPANY_ID.ToString();
            return View();
        }
        public ActionResult RolePowerEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            role roleinfo = new role();
            if (action == "add")
            {
                Id = 0;
            }
            else
            {
                roleinfo = BLLSession.Current.roleBLL.GetListBy(c => c.ROLE_ID == Id).FirstOrDefault();
            }

            ViewData.Model = roleinfo;
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }
        public ActionResult RoleDetail(int page, int rows, role role, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "ROLE_ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;
            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            SearchCondition condition1 = new SearchCondition() { Field = "ROLE_NAME", Operator = "!=", Value = "超级管理员" };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);
            listSearch.Add(condition1);
            Expression<Func<role, bool>> exp = SearchExpression.GetSearchExpression<role>(role, listSearch, SearchContains, "ROLE_NAME");

            var list = BLLSession.Current.roleBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.roleDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetPowerById(int Id)
        {
            var user = BLLSession.Current.roleBLL.GetListBy(c => c.ROLE_ID == Id).FirstOrDefault();
            return Json(user, JsonRequestBehavior.AllowGet);
        }
        public ActionResult EditPower(role role)
        {
            //if (OperateContext.userInfo.COMPANY_ID != 2)
            //{
            //    return Json(new Message() { state = "error", msg = "没有操作权限" });
            //}
            role.IS_Company = role.IS_Company == null ? false : true;
            role.IS_Message = role.IS_Message == null ? false : true;
            role.IS_Monitor = role.IS_Monitor == null ? false : true;
            role.IS_Report = role.IS_Report == null ? false : true;
            role.IS_Resource = role.IS_Resource == null ? false : true;
            role.IS_Setting = role.IS_Setting == null ? false : true;
            BLLSession.Current.roleBLL.Modify(role, "IS_Company", "IS_Message", "IS_Monitor", "IS_Report", "IS_Resource", "IS_Setting");
            return Json(new Message() { state = "success" });

        }
        #endregion


        #region---------------------------------公司信息管理----------------------------------------------
        public ActionResult CompanyInfo()
        {
            int Id = OperateContext.userInfo.COMPANY_ID;
            ViewData["Id"] = Id;
            company companyinfo = BLLSession.Current.companyBLL.GetListBy(c => c.COMPANY_ID == Id).FirstOrDefault();
            ViewData.Model = companyinfo;
            return View();
        }
        public ActionResult CompanyInfoEdit()
        {
            int Id = Commons.String2Int32(Request.QueryString["Id"].ToString());
            string width = Request.QueryString["width"].ToString();
            string action = Request.QueryString["action"].ToString();
            ViewData["Id"] = Id;
            ViewData["width"] = width;
            ViewData["action"] = action;
            return View();
        }

        public ActionResult GetCompanyById(int Id)
        {
            var company = BLLSession.Current.companyBLL.GetListBy(c => c.COMPANY_ID == Id).FirstOrDefault();
            return Json(company, JsonRequestBehavior.AllowGet);
        }

        public ActionResult EditCompany(company company)
        {
            BLLSession.Current.companyBLL.Modify(company, "COMPANY_FULLNAME", "COMPANY_NAME", "COPANNY_CHARGE", "COMPANY_PHONE", "COMPANY_CTIY");
            return Json(new Message() { state = "success" });
        }
        #endregion


        #region ---------------------------------物料信息----------------------------------------------
        public ActionResult MaterialList()
        {
            return View();
        }

        public ActionResult MaterialListDetail(int page, int rows, material material, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "MATERIAL_ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);

            Expression<Func<material, bool>> exp = SearchExpression.GetSearchExpression<material>(material, listSearch, SearchContains, "M_CODE", "M_COMPANY", "M_NAME", "M_SPECIFICATION", "M_TYPE", "M_UNIQUEID");

            var list = BLLSession.Current.materialBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.materialDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult MaterialListEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            material info = new material();
            if (action == "add")
            {
                Id = 0;
            }
            else
            {
                info = BLLSession.Current.materialBLL.GetListBy(c => c.MATERIAL_ID == Id).FirstOrDefault();
            }
            ViewBag.materiallist = info;
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult GetMaterialInfoById(int Id)
        {
            var product = BLLSession.Current.materialBLL.GetListBy(c => c.MATERIAL_ID == Id).FirstOrDefault();
            return Json(product, JsonRequestBehavior.AllowGet);
        }
        public ActionResult AddMaterial(material info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            info.COMPANY_ID = OperateContext.userInfo.COMPANY_ID;
            BLLSession.Current.materialBLL.Add(info);
            return Json(new Message() { state = "success" });
        }

        public ActionResult EditMaterial(material info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            info.COMPANY_ID = OperateContext.userInfo.COMPANY_ID;
            BLLSession.Current.materialBLL.Modify(info, "M_CODE", "M_SPECIFICATION", "M_IMGURL", "M_TYPE", "M_STATUS", "COMPANY_ID", "M_COMPANY", "M_IMGURL", "CREATED_TIME", "CREATED_BY");
            return Json(new Message() { state = "success" });
        }

        public ActionResult DeleteMaterialInfoById(int Id)
        {
            BLLSession.Current.materialBLL.DelBy((d => d.MATERIAL_ID == Id));
            Message msg = new Message() { state = "success" };
            return Json(msg, JsonRequestBehavior.AllowGet);
        }
        public ActionResult upload(string url)
        {
            HttpPostedFileBase files = Request.Files[0];//这里只能用<input type="file" />才能有效果,因为服务器控件是HttpInputFile类型

            string imgsize = string.Empty;
            string error = string.Empty;
            string FileNames = string.Empty;
            string TrueFileNames = string.Empty;
            string imgurl = string.Empty;
            string str = string.Empty;
            var tempObj = new object();
            if (files!=null)
            {
                long fileSize = 0;
                fileSize = files.ContentLength;
                string PicSizeVar = "1024";
                //上传图片是否已超过了一个限定的值
                if (fileSize > long.Parse(PicSizeVar) * 1024)
                {
                    error = "图片超过1M，请重新上传";
                    tempObj = new { code = "error", imgurl = url, message = error };
                }
                else
                {
                    double dbsize = Convert.ToDouble(fileSize) / 1024;
                    imgsize = dbsize.ToString("0.00") + "KB";
                    //获取上传文件的名称  
                    string s = files.FileName.ToString(); //files.FileName.Replace(" ", "");
                    //截取获得上传文件的名称(ie上传会把绝对路径也连带上，这里只得到文件的名称)  
                    str = s.Substring(s.LastIndexOf("\\") + 1);
                    FileNames = str;
                    Random r = new Random();
                    str = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + r.Next(1000).ToString() + "_" + str;//重命名文件，保证每个文件的名称都是唯一的
                    TrueFileNames = str;
                    string path = "~/uploadimages/" + str;
                    files.SaveAs(Server.MapPath(path));
                    imgurl = @"/uploadimages/" + str;
                    tempObj = new { code = "success", imgsize = imgsize, imgurl = imgurl, message = "上传成功！" };
                }
                /*
                 *error:错误信息
                 *imgsize文件大小 
                 *imgurl图片真实地址
                 *FileNames文件重命名后的名字
                 *TrueFileNames真实的文件名称
                 */
             //string res = "{ \"error\":\"" + error + "\", \"imgsize\":\"" + imgsize + "\",\"imgurl\":\"" + imgurl + "\",\"FileNames\":\"" + FileNames + "\",\"TrueFileNames\":\"" + TrueFileNames + "\"}";

            }
            else
            {
                tempObj = new { code = "error",imgurl="", message = "上传失败！" };
            }
            return Json(tempObj, JsonRequestBehavior.AllowGet);
        }


        #endregion


        #region =======================================消息设置=======================================
        public ActionResult MessageSet()
        {
            return View();
        }

        public ActionResult MessageSetDetail(int page, int rows, systemvariable systemvariable, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);
            Expression<Func<systemvariable, bool>> exp = SearchExpression.GetSearchExpression<systemvariable>(systemvariable, listSearch, SearchContains, "VARIABLE_NAME", "VARIABLE_CODE");

            var list = BLLSession.Current.systemvariableBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.systemvariableDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult MessageSetEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult GetMessageSetById(int Id)
        {
            var user = BLLSession.Current.systemvariableBLL.GetListBy(c => c.ID == Id).FirstOrDefault();
            return Json(user, JsonRequestBehavior.AllowGet);
        }

        public ActionResult EditMessageSet(systemvariable systemvariable)
        {
            systemvariable.CREATED_BY = OperateContext.userInfo.NAME;
            systemvariable.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.systemvariableBLL.Modify(systemvariable,"VARIABLE_VALUE", "CREATED_TIME", "CREATED_BY");
            return Json(new Message() { state = "success" });
        }
        #endregion

        #region =======================================产品领料模式=======================================
        public ActionResult MaterialProductMode()
        {
            return View();
        }

        public ActionResult MaterialProductModeDetail(int page, int rows, materialbyproduct materialbyproduct, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);
            Expression<Func<materialbyproduct, bool>> exp = SearchExpression.GetSearchExpression<materialbyproduct>(materialbyproduct, listSearch, SearchContains, "PRODUCTNAME", "PROCESSNAME", "MACHINENAME");

            var list = BLLSession.Current.materialbyproductBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.materialbyproductDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult MaterialProductModeEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            if (action == "add")
            {
                Id = 0;
            }

            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult GetMaterialProductInfoById(int Id)
        {
            var info = BLLSession.Current.materialbyproductBLL.GetListBy(c => c.ID == Id).FirstOrDefault();
            return Json(info, JsonRequestBehavior.AllowGet);
        }
        public ActionResult AddMaterialProductMode(materialbyproduct info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            info.COMPANY_ID = OperateContext.userInfo.COMPANY_ID;
            BLLSession.Current.materialbyproductBLL.Add(info);
            return Json(new Message() { state = "success" });
        }

        public ActionResult EditMaterialProductMode(materialbyproduct info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.materialbyproductBLL.Modify(info, "PRODUCTNAME", "PROCESSNAME", "MACHINENAME", "CREATED_TIME", "CREATED_BY");
            return Json(new Message() { state = "success" });
        }

        public ActionResult DeleteMaterialProductModeInfoById(int Id)
        {
            BLLSession.Current.materialbyproductBLL.DelBy((d => d.ID == Id));
            Message msg = new Message() { state = "success" };
            return Json(msg, JsonRequestBehavior.AllowGet);
        }


        public ActionResult MaterialProductModeAdd()
        {
            return View();
        }
        public ActionResult MaterialProductModeMaterialAdd()
        {
            return View();
        }
        public ActionResult GetProductModeMaterialListById(int Id)
        {
            var info = DBNull.Value; //BLLSession.Current.vmaterialbyproductlist.GetListBy(c => c.ID == Id).FirstOrDefault();
            return Json(info, JsonRequestBehavior.AllowGet);
        }

        
        #endregion

        #region =======================================机床领料模式=======================================

        public ActionResult MaterialMachineMode()
        {
            return View();
        }

        public ActionResult MaterialMachineModeDetail(int page, int rows, materialbymachine materialbymachine, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);
            Expression<Func<materialbymachine, bool>> exp = SearchExpression.GetSearchExpression<materialbymachine>(materialbymachine, listSearch, SearchContains, "VARIABLE_NAME", "VARIABLE_CODE");

            var list = BLLSession.Current.materialbymachineBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.materialbymachineDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult MaterialMachineModeEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            if (action == "add")
            {
                Id = 0;
            }

            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult GetMaterialMachineModeInfoById(int Id)
        {
            var info = BLLSession.Current.materialbymachineBLL.GetListBy(c => c.ID == Id).FirstOrDefault();
            return Json(info, JsonRequestBehavior.AllowGet);
        }
        public ActionResult AddMaterialMachineMode(materialbymachine info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            info.COMPANY_ID = OperateContext.userInfo.COMPANY_ID;
            BLLSession.Current.materialbymachineBLL.Add(info);
            return Json(new Message() { state = "success" });
        }

        public ActionResult EditMaterialMachineMode(materialbymachine info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.materialbymachineBLL.Modify(info, "FACTORYNAME", "WORKSHOPNAME", "LINENAME", "MACHINENAME", "CREATED_TIME", "CREATED_BY");
            return Json(new Message() { state = "success" });
        }

        public ActionResult DeleteMaterialMachineModeInfoById(int Id)
        {
            BLLSession.Current.materialbymachineBLL.DelBy((d => d.ID == Id));
            Message msg = new Message() { state = "success" };
            return Json(msg, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region===============================智能单元======================================
        public ActionResult CabinetListInfo()
        {
            return View();
        }
        public ActionResult CabinetDetail(int page, int rows, vcabinet vcabinet, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "COMPANY_ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);

            Expression<Func<vcabinet, bool>> exp = SearchExpression.GetSearchExpression<vcabinet>(vcabinet, listSearch, SearchContains, "CAB_NAME", "COMPANY_NAME", "SUBPRODUCT_NO", "PRODUCT_NO");

            var list = BLLSession.Current.vcabinetBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.vcabinetDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult CabinetEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            vcabinet info = new vcabinet();
            if (action == "add")
            {
                Id = 0;
            }
            else
            {
                info = BLLSession.Current.vcabinetBLL.GetListBy(c => c.CAB_ID == Id).FirstOrDefault();
            }
            var subproductlist = BLLSession.Current.vsubproductBLL.GetListBy(c => 1 == 1).ToList();
            ViewBag.subproductlist = subproductlist;
            var materiallist = BLLSession.Current.materialBLL.GetListBy(c => c.COMPANY_ID== OperateContext.userInfo.COMPANY_ID).ToList();
            ViewBag.materiallist = materiallist;
            var userlist = BLLSession.Current.userBLL.GetListBy(c => c.COMPANY_ID == OperateContext.userInfo.COMPANY_ID).ToList();
            ViewBag.userlist = userlist;
            ViewData.Model = info;
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult GetCabinetInfoById(int Id)
        {
            var cabinet = BLLSession.Current.vcabinetBLL.GetListBy(c => c.CAB_ID == Id).FirstOrDefault();
            return Json(cabinet, JsonRequestBehavior.AllowGet);
        }

        public ActionResult EditCabinet(vcabinet info)
        {
            cabinet cab = new cabinet();
            cab.CAB_STATUS = info.CAB_STATUS;
            cab.CAB_TYPE = info.CAB_TYPE;
            cab.CAB_MARTERIAL_ID = info.CAB_MARTERIAL_ID;
            material m = new material();
            m = BLLSession.Current.materialBLL.GetListBy(c => c.M_CODE == info.CAB_MARTERIAL_ID).FirstOrDefault();
            if (m != null)
            {
                cab.CAB_MARTERIAL = m.M_TYPE + "|"+m.M_NUMBER+"|" + m.M_SPECIFICATION;
            }
            cab.CAB_MARTERIAL_NUMBER = info.CAB_MARTERIAL_NUMBER;
            cab.CAB_USER_ID = info.CAB_USER_ID;
            user u = new user();
            u= BLLSession.Current.userBLL.GetListBy(c => c.USER_ID == info.CAB_USER_ID).FirstOrDefault();
            if (u != null)
            {
                cab.CAB_USER_NAME = u.NAME;
            }
            cab.CREATED_BY = OperateContext.userInfo.NAME;
            cab.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BLLSession.Current.cabinetBLL.ModifyBy(cab, c => c.CAB_ID == info.CAB_ID, "CAB_STATUS", "CAB_TYPE", "CAB_MARTERIAL_ID", "CAB_MARTERIAL", "CAB_MARTERIAL_NUMBER", "CAB_USER_ID", "CAB_USER_NAME", "CREATED_TIME", "CREATED_BY");
            return Json(new Message() { state = "success" });
        }

        #endregion

        #region =======================================安全库存设置=======================================
        public ActionResult SafetyStock()
        {
            return View();
        }

        public ActionResult SafetyStockDetail(int page, int rows, vmaterialsafetystock materialsafetystock, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "ID" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);
            Expression<Func<vmaterialsafetystock, bool>> exp = SearchExpression.GetSearchExpression<vmaterialsafetystock>(materialsafetystock, listSearch, SearchContains, "M_CODE", "PRODUCT_NO", "M_SPECIFICATION");

            var list = BLLSession.Current.vmaterialsafetystockBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.vmaterialsafetystockDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SafetyStockEdit()
        {
            string action = Request.QueryString["action"].ToString();
            int Id = Commons.String2Int32(Request.QueryString["Id"]);
            string width = Request.QueryString["width"].ToString();
            if (action == "add")
            {
                Id = 0;
            }
            var materiallist = BLLSession.Current.materialBLL.GetListBy(c => c.COMPANY_ID == OperateContext.userInfo.COMPANY_ID).ToList();
            ViewBag.materiallist = materiallist;
            var productlist = BLLSession.Current.vproductBLL.GetListBy(c => c.COMPANY_ID == OperateContext.userInfo.COMPANY_ID).ToList();
            ViewBag.productlist = productlist;
            ViewData["action"] = action;
            ViewData["Id"] = Id.ToString();
            ViewData["width"] = width;
            return View();
        }

        public ActionResult GetSafetyStockInfoById(int Id)
        {
            var info = BLLSession.Current.vmaterialsafetystockBLL.GetListBy(c => c.ID == Id).FirstOrDefault();
            return Json(info, JsonRequestBehavior.AllowGet);
        }
        public ActionResult AddSafetyStock(materialsafetystock info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            info.COMPANY_ID = OperateContext.userInfo.COMPANY_ID;
            info.PRODUCT_NO = BLLSession.Current.vproductBLL.GetListBy(c => c.PRODUCT_ID == info.PRODUCT_ID).FirstOrDefault().PRODUCT_NO;
            info.M_CODE = BLLSession.Current.vmaterialBLL.GetListBy(c => c.MATERIAL_ID == info.MATERIAL_ID).FirstOrDefault().M_CODE;
            BLLSession.Current.materialsafetystockBLL.Add(info);
            return Json(new Message() { state = "success" });
        }

        public ActionResult EditSafetyStock(materialsafetystock info)
        {
            info.CREATED_BY = OperateContext.userInfo.NAME;
            info.CREATED_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            info.PRODUCT_NO = BLLSession.Current.vproductBLL.GetListBy(c => c.PRODUCT_ID == info.PRODUCT_ID).FirstOrDefault().PRODUCT_NO;
            info.M_CODE = BLLSession.Current.vmaterialBLL.GetListBy(c => c.MATERIAL_ID == info.MATERIAL_ID).FirstOrDefault().M_CODE;
            info.COMPANY_ID = OperateContext.userInfo.COMPANY_ID;
            BLLSession.Current.materialsafetystockBLL.Modify(info, "MATERIAL_ID", "M_CODE", "PRODUCT_NO", "PRODUCT_ID", "SAFETY_STOCK", "COMPANY_ID", "CREATED_TIME", "CREATED_BY");
            return Json(new Message() { state = "success" });
        }

        public ActionResult DeleteSafetyStockInfoById(int Id)
        {
            BLLSession.Current.materialsafetystockBLL.DelBy((d => d.ID == Id));
            Message msg = new Message() { state = "success" };
            return Json(msg, JsonRequestBehavior.AllowGet);
        }
        #endregion

    }
}
